package com.oa.hyperion.api.v1;

import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.hyperion.entity.Controller;
import com.oa.hyperion.entity.Outline;
import com.oa.hyperion.entity.Template;
import com.oa.hyperion.service.CustomFormService;
import com.oa.hyperion.utils.JwtUtils;
import com.oa.hyperion.vo.DetailVo;
import com.oa.hyperion.vo.OutlineQuery;
import com.oa.hyperion.vo.TemplateUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Liu
 * @date 2022年01月18日 9:35
 */
@RestController
@RequestMapping("v1/form")
@Api(value = "自定义表单接口", tags = "自定义表单接口")
public class CustomFormController {
    private final CustomFormService customFormService;
    private final RedisTemplate<Object, Object> redisTemplate;
    public CustomFormController(CustomFormService customFormService, RedisTemplate<Object, Object> redisTemplate) {
        this.customFormService = customFormService;
        this.redisTemplate = redisTemplate;
    }

    @ApiOperation("获取所有的模板")
    @GetMapping("templates")
    public List<Template> getAllTemplates() {
        return customFormService.getAllTemplate();
    }

    @ApiOperation("获取指定模板ID的内容")
    @GetMapping("templates/{id}")
    public Template getTemplate(@ApiParam(name = "id", value = "模板的ID") @PathVariable Integer id) {
        return customFormService.getTemplate(id);
    }

    @ApiOperation("获取指定模板下的所有概要")
    @GetMapping("templates/{id}/outlines")
    public List<Outline> getTemplatesOutline(@ApiParam(name = "id", value = "模板的ID") @PathVariable Integer id) {
        return customFormService.getAllOutlineByTemplate(id);
    }

    @ApiOperation("保存模板")
    @PostMapping("templates")
    public String addTemplate(@ApiParam @RequestBody Template template,@RequestHeader("Authorization") String token) {
        Map<String, Claim> claim = JwtUtils.getClaim(token.split(" ")[1]);
        assert claim != null;
        Integer id = claim.get("id").asInt();
        template.setCreateTime(new Date());
        template.setStatus("y");
        template.setCreator(id);
        customFormService.saveTemplate(template);
        return "保存成功";
    }

    @ApiOperation("修改模板信息")
    @PutMapping("templates/{id}")
    public String updateTemplate(@ApiParam("修改的模板ID") @PathVariable Integer id, @ApiParam @RequestBody TemplateUpdateVo templateUpdateVo) {
        customFormService.updateTemplate(id, templateUpdateVo);
        return "修改成功";
    }

    @ApiOperation("删除模板信息")
    @DeleteMapping("templates/{id}")
    public String deleteTemplate(@ApiParam("要删除的模板ID") @PathVariable Integer id) {
        customFormService.deleteTemplate(id);
        return "删除成功";
    }

    @ApiOperation("获取详细信息")
    @GetMapping("details/{id}")
    public List<DetailVo> getDetail(@ApiParam("要获取概要的ID")@PathVariable Integer id) {
        return customFormService.getOutlineDetail(id);
    }

    @ApiOperation("获取简要信息")
    @GetMapping("outlines/{id}")
    public Outline getOutlineById(@PathVariable Integer id) {
        return customFormService.getOutline(id);
    }

    @ApiOperation("获取详细信息")
    @GetMapping("outlines/{id}/with-data")
    public Outline getOutline(@PathVariable Integer id) {
        return customFormService.getOutline(id);
    }

    @ApiOperation("保存填写的表单内容")
    @PostMapping("templates/{id}/outlines")
    public String saveOutline(@PathVariable Integer id, @RequestBody Outline outline,@RequestHeader("Authorization") String token) {
        outline.setApplicant(JwtUtils.getId(token));
        outline.setCreateTime(new Date());
        outline.setStatus("w");
        outline.setCheckStatus("w");
        outline.setTemplateId(id);

        customFormService.saveOutline(outline);
        return "保存成功";
    }

    @ApiOperation("获取所有的控件")
    @GetMapping("controllers")
    public List<Controller> getAllControllers() {
        return customFormService.getAllController();
    }

    @ApiOperation("获取当前用户所有的自定义表单")
    @GetMapping("outlines")
    public Page<Outline> getAllOutline(@RequestHeader("Authorization") String token, OutlineQuery outlineQuery) {
        Integer id = JwtUtils.getId(token);
        return customFormService.getOutlineByUId(id, outlineQuery);
    }

    @ApiOperation("逻辑删除概要")
    @DeleteMapping("outlines/{id}")
    public String deleteOutline(@PathVariable Integer id) {
        customFormService.deleteOutline(id);
        return "删除成功";
    }

    @ApiOperation("更改概要状态")
    @PutMapping("outlines/{id}")
    public String updateOutline(@PathVariable Integer id) {
        customFormService.updateOutline(id);
        return "更新成功";
    }
}
